package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.INoticeService;
import cn.wolfcode.util.UserContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private INoticeService noticeService;

    @RequestMapping("/empLogin")
    @ResponseBody
    public JsonResult login(String username, String password, HttpSession session){
        JsonResult result = new JsonResult();
        Employee employee = employeeService.checkEmp(username, password);
        UserContextUtil.setCurrentEmployee(employee);
        UserContextUtil.setCurrentPermissions(employeeService.selectPermissions(employee.getId()));
        UserContextUtil.setCurrentUnReadNotices(noticeService.queryNoAlready());
        return result;
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "redirect:/login.html";
    }

    @RequestMapping("/noPermission")
    public String noPermission(){
        return "common/nopermission";
    }

    @RequestMapping("/error")
    public String error(){
        return "common/error";
    }
}
